package com.safewind.wx.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class DateUtil {

    public static String format(Date date, String str) {
        SimpleDateFormat sdf = new SimpleDateFormat(str);
        return sdf.format(date);
    }

    public static Date parse(String str, String str2) {
        str = str.replace("T", " ");
        SimpleDateFormat sdf = new SimpleDateFormat(str2);
        Date d = null;
        try {
            d = sdf.parse(str);
        } catch (ParseException e) {
            d = new Date(Long.valueOf(str));
        }
        return d;
    }

    public static String getTimeQuantum(Date start, Date end) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(start);
        int month1 = cal1.get(Calendar.MONTH) + 1;
        int day1 = cal1.get(Calendar.DAY_OF_MONTH);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(end);
        int month2 = cal2.get(Calendar.MONTH) + 1;
        int day2 = cal2.get(Calendar.DAY_OF_MONTH);

        StringBuffer res = new StringBuffer();
        if (month1 == month2) {
            res.append(month1 + "月");
            if (day1 == day2) {
                res.append(day1 + "日");
            } else {
                res.append(day1 + "日-" + day2 + "日");
            }
        } else {
            res.append(month1 + "月");
            res.append(day1 + "日-");
            res.append(month2 + "月");
            res.append(day2 + "日");
        }
        return res.toString();
    }

    public static int isContainedToday(Date start, Date end) {
        // 当前时间毫秒数
        long current = System.currentTimeMillis();
        // 今天零点零分零秒的毫秒数
        long zero = current / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();
        // 今天23点59分59秒的毫秒数
        long twelve = zero + 24 * 60 * 60 * 1000 - 1;
        if (!(start.getTime() > twelve || end.getTime() < zero)) {
            return 1;
        }

        return 0;
    }

    public static int getMondayPlus() {
        Calendar cd = Calendar.getInstance();
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == 1) {
            dayOfWeek = 6;
        } else {
            dayOfWeek = dayOfWeek - 2;
        }
        return dayOfWeek;
    }

    // 获得当前周- 周一的日期
    public static String getMonday() {
        int mondayPlus = getMondayPlus();
        GregorianCalendar currentDate = new GregorianCalendar();
        currentDate.add(Calendar.DATE, -mondayPlus);
        Date day = currentDate.getTime();
        return format(day, "yyyy-MM-dd");
    }

    // 获得当前周- 周日 的日期
    public static String getSunday() {
        int mondayPlus = getMondayPlus();
        GregorianCalendar currentDate = new GregorianCalendar();
        currentDate.add(Calendar.DATE, 6 - mondayPlus);
        Date day = currentDate.getTime();
        return format(day, "yyyy-MM-dd");
    }

}
